<?php

namespace app\model\v1\repair;

use app\model\order\StoreOrder;
use app\model\store\DeliveryService;
use app\model\user\User;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;

class WorkerExamine extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];


    public function user()
    {
        return $this->hasOne (User::class,'uid','user_id');
    }

    public function repair()
    {
        return $this->hasOne (RepairService::class,'uid','user_id');
    }

    public function order()
    {
        return $this->hasOne (StoreOrder::class,'order_id','order_id');
    }

    public function surveyor()
    {
        return $this->hasOne (SurveyorService::class,'uid','user_id');
    }

    public function delivery()
    {
        return $this->hasOne (DeliveryService::class,'uid','user_id');
    }


    /**
     * 勘察员绩效管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getSurveyorExamineList($param = [], $ids = [])
    {
        $data["data"] = self::field("*,
            case when status=1 then '启用' 
                when status=0 then '禁用' 
                end as status_text
            ")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->with([
                'user'=> function($query) {
                    $query->field('uid,nickname,real_name,phone');
                },
                'order'=> function($query) {
                    $query->field('order_id,FROM_UNIXTIME(pay_time) as pay_time,pay_price');
                },
                'repair'=> function($query) {
                    $query->field('id,uid,avatar,nickname,phone');
                },
                'surveyor'=> function($query) {
                    $query->field('id,uid,avatar,nickname,phone');
                },
                'delivery'=> function($query) {
                    $query->field('id,uid,avatar,nickname,phone');
                },
            ])
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field("*")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }

    /**
     * 勘察员绩效管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateSurveyorExamine($param = [])
    {
        if(!empty($param["user_id"]))           $update["user_id"] = $param["user_id"];
        if(!empty($param["single_quantity"]))   $update["single_quantity"] = $param["single_quantity"];
        if(!empty($param["performance"]))       $update["performance"] = $param["performance"];
        if(!empty($param["Bad"]))               $update["Bad"] = $param["Bad"];
        if(!empty($param["esteem"]))            $update["esteem"] = $param["esteem"];
        if(!empty($param["substract"]))         $update["substract"] = $param["substract"];
        if(!empty($param["settlement"]))        $update["settlement"] = $param["settlement"];
        if(!empty($param["ticheng"]))           $update["ticheng"] = $param["ticheng"];
        if(!empty($param["order_id"]))          $update["order_id"] = $param["order_id"];

        return self::where("id", $param['id'])->update($update);
    }



    /**
     * 勘察员绩效管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addSurveyorExamine($param = [])
    {
        $add = [
            "user_id" => $param["user_id"] ?? 0,
            "single_quantity" => $param["single_quantity"] ?? '',
            "performance" => $param["performance"] ?? '',
            "Bad" => $param["Bad"] ?? '',
            "esteem" => $param["esteem"] ?? '',
            "substract" => $param["substract"] ?? '',
            "settlement" => $param["settlement"] ?? '',
            "ticheng" => $param["ticheng"] ?? '',
            "order_id" => $param["order_id"] ?? '',
        ];
        return self::insert($add);
    }


    /**
     * 勘察员绩效管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableSurveyorExamine($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }



    /**
     * 勘察员绩效汇总列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getSummaryExamineList($param = [], $ids = [])
    {
        $data["data"] = self::field("user_id, sum(single_quantity) as single_sum, sum(performance) as performance_sum, 
				sum(Bad) as Bad_sum, sum(esteem) as esteem_sum, sum(substract) as substract_sum, 
				sum(settlement) as settlement_sum, sum(ticheng) as ticheng_sum
            ")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->with([
                'user'=> function($query) {
                    $query->field('uid,nickname,real_name,phone');
                },
                'surveyor'=> function($query) {
                    $query->field('id,uid,avatar,nickname,phone');
                },
            ])
            ->group("user_id")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();
        if(!empty($data["data"])){
            foreach ($data["data"] as $key => &$val) {
                $val["settlement_sum"] = number_format($val["ticheng_sum"] - $val["substract_sum"], 2);
            }
        }

        $data["count"] = self::field("user_id, sum(single_quantity) as single_sum, sum(performance) as performance_sum, 
				sum(Bad) as Bad_sum, sum(esteem) as esteem_sum, sum(substract) as substract_sum, 
				sum(settlement) as settlement_sum, sum(ticheng) as ticheng_sum
            ")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->group("user_id")
            ->count();

        return $data;
    }



    /**
     * 维修工绩效汇总列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getSummaryRepairList($param = [], $ids = [])
    {
        $data["data"] = self::field("user_id, sum(single_quantity) as single_sum, sum(performance) as performance_sum, 
				sum(Bad) as Bad_sum, sum(esteem) as esteem_sum, sum(substract) as substract_sum, 
				sum(settlement) as settlement_sum, sum(ticheng) as ticheng_sum
            ")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->with([
                'user'=> function($query) {
                    $query->field('uid,nickname,real_name,phone');
                },
                'repair'=> function($query) {
                    $query->field('id,uid,avatar,nickname,phone');
                },
            ])
            ->group("user_id")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();
        if(!empty($data["data"])){
            foreach ($data["data"] as $key => &$val) {
                $val["settlement_sum"] = number_format($val["ticheng_sum"] - $val["substract_sum"], 2);
            }
        }

        $data["count"] = self::field("user_id, sum(single_quantity) as single_sum, sum(performance) as performance_sum, 
				sum(Bad) as Bad_sum, sum(esteem) as esteem_sum, sum(substract) as substract_sum, 
				sum(settlement) as settlement_sum, sum(ticheng) as ticheng_sum
            ")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->group("user_id")
            ->count();

        return $data;
    }



    /**
     * 送水工绩效汇总列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getSummaryWaterList($param = [], $ids = [])
    {
        $data["data"] = self::field("user_id, sum(single_quantity) as single_sum, sum(performance) as performance_sum, 
				sum(Bad) as Bad_sum, sum(esteem) as esteem_sum, sum(substract) as substract_sum, 
				sum(settlement) as settlement_sum, sum(ticheng) as ticheng_sum
            ")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->with([
                'user'=> function($query) {
                    $query->field('uid,nickname,real_name,phone');
                },
                'delivery'=> function($query) {
                    $query->field('id,uid,avatar,nickname,phone');
                },
            ])
            ->group("user_id")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();
        if(!empty($data["data"])){
            foreach ($data["data"] as $key => &$val) {
                $val["settlement_sum"] = number_format($val["ticheng_sum"] - $val["substract_sum"], 2);
            }
        }

        $data["count"] = self::field("user_id, sum(single_quantity) as single_sum, sum(performance) as performance_sum, 
				sum(Bad) as Bad_sum, sum(esteem) as esteem_sum, sum(substract) as substract_sum, 
				sum(settlement) as settlement_sum, sum(ticheng) as ticheng_sum
            ")
            ->whereNull("delete_time")
            ->whereIn("user_id", $ids)
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("order_id",'like', '%'.$param["word"].'%');
            })
            ->group("user_id")
            ->count();

        return $data;
    }



}